package cn.happiness.frog.skill.xiaoai;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@NoArgsConstructor
@Data
public class Response {
	@JsonProperty("version")
	private String version = "1.0";
	@JsonProperty("response")
	private ResponseDTO response = new ResponseDTO();
	@JsonProperty("is_session_end")
	private Boolean isSessionEnd = false;

	public Response notUnderstand() {
		this.response.notUnderstand();
		return this;
	}

	public Response ttsReply(String text) {
		this.response.tts(text);
		return this;
	}

	public Response audioReply(String url) {
		this.response.audio(url);
		return this;
	}

	public Response speakReply(String text) {
		this.response.speak(text);
		return this;
	}

	public Response exit(String text) {
		this.isSessionEnd = true;
		if (text != null) {
			this.response.speak(text);
		}
		return this;
	}

	@NoArgsConstructor
	@Data
	public static class ResponseDTO {
		@JsonProperty("open_mic")
		private Boolean openMic = false;
		@JsonProperty("not_understand")
		private Boolean notUnderstand = false;

		@JsonProperty("to_speak")
		private ToSpeak toSpeak;

		@JsonProperty("directives")
		private List<DirectiveDTO> directives;

		public ResponseDTO notUnderstand() {
			this.notUnderstand = true;
			return this;
		}

		public ResponseDTO speak(String text) {
			this.openMic = true;
			this.toSpeak = new ToSpeak();
			this.toSpeak.text = text;
			return this;
		}

		public ResponseDTO audio(String url) {
			directives = Optional.ofNullable(directives).orElse(new ArrayList<>());
			DirectiveDTO.AudioItemDTO.StreamDTO stream = new DirectiveDTO.AudioItemDTO.StreamDTO();
			stream.setUrl(url);
			DirectiveDTO.AudioItemDTO audioItem = new DirectiveDTO.AudioItemDTO();
			audioItem.setStream(stream);
			DirectiveDTO directive = new DirectiveDTO();
			directive.setType("audio");
			directive.setAudioItem(audioItem);
			directives.add(directive);
			return this;
		}

		public ResponseDTO tts(String text) {
			directives = Optional.ofNullable(directives).orElse(new ArrayList<>());
			DirectiveDTO directive = new DirectiveDTO();
			directive.setType("tts");
			DirectiveDTO.TTSItemDTO ttsItem = new DirectiveDTO.TTSItemDTO();
			ttsItem.setText(text);
			directive.setTtsItem(ttsItem);
			directives.add(directive);
			return this;
		}

		@Data
		public static class ToSpeak {
			private Integer type = 0;
			private String text;
		}

		@NoArgsConstructor
		@Data
		public static class DirectiveDTO {
			@JsonProperty("type")
			private String type;
			@JsonProperty("audio_item")
			private AudioItemDTO audioItem;
			@JsonProperty("tts_item")
			private TTSItemDTO ttsItem;

			@NoArgsConstructor
			@Data
			public static class AudioItemDTO {
				@JsonProperty("stream")
				private StreamDTO stream;

				@NoArgsConstructor
				@Data
				public static class StreamDTO {
					@JsonProperty("url")
					private String url;
				}
			}

			@NoArgsConstructor
			@Data
			public static class TTSItemDTO {
				private String type = "text";
				private String text;
			}
		}
	}
}
